package cn.edu.pzhu.javaweb.controller;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.edu.pzhu.javaweb.pojo.Result;
import cn.edu.pzhu.javaweb.pojo.Userinfo;
import cn.edu.pzhu.javaweb.service.UserService;
import cn.edu.pzhu.javaweb.service.imp.UserServiceImp;

/**
 * Servlet implementation class ShowUserInfoServlet
 */
@WebServlet("/userinfo")
public class ShowUserInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private UserService userService =new UserServiceImp();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowUserInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = (String)request.getSession().getAttribute("username");
		Result<Userinfo> result = userService.getPersonalInfo(username);
		Userinfo userinfo =result.getData();
		request.setAttribute("userinfo", userinfo);//将用户信息存入request域的userinfo中	
		String[] types = JSON.parseObject(userinfo.getType(),String[].class);//将userinfo中的关注类型（字符串）转换为数组
		request.setAttribute("types",types); //将关注类型保存起来，让JSP页面读取
		System.out.println(Arrays.toString(types));
		request.getRequestDispatcher("showuserinfo.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
